# Import the sync_playwright function from the sync_api module of Playwright.
from playwright.sync_api import sync_playwright, Route
from datetime import datetime
import re
BROWSER_TIMEOUT: int = 60*1000 #miliseconds
def abort_handle_route(route:Route, request):
    route.abort()

# Start a new session with Playwright using the sync_playwright function.
with sync_playwright() as playwright:
    # Connect to an existing instance of Chrome using the connect_over_cdp method.
    browser = playwright.chromium.connect_over_cdp("http://localhost:9223")

    # Retrieve the first context of the browser.
    default_context = browser.contexts[0]

    page = default_context.new_page()
    page.route(re.compile(r"\.(jpg|png|svg)$"), abort_handle_route)
    url = f"https://cn.bing.com/"
    page.goto(url, timeout=BROWSER_TIMEOUT)
    page.wait_for_load_state('domcontentloaded')
    print(f'{datetime.now()} domcontentloaded')
